package com.epfresh.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.activity.CenterListActivity;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.imageaware.ImageViewAware;
import com.epfresh.api.utils.T;
import com.epfresh.basedialog.CustomDialog;
import com.epfresh.bean.Address;
import com.epfresh.bean.CenterListEntity;
import com.epfresh.global.BaseApplication;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wzh on 2016/5/6.
 */
public class CenterListAdapter extends BaseAdapter implements View.OnClickListener {

    public Context context;
    private ArrayList<CenterListEntity> list;
    public CenterListAdapter(Context context, ArrayList<CenterListEntity> list) {
        this.context = context;
        this.list = list;
/*        for(int i=0;i<list.size();i++){
            if(list.get(i).getStore().getBindingStatus().equals("1")){
            }else if(list.get(i).getStore().getBindingStatus().equals("2")){
                //bingingFlag = true;
            }else {
                //bindFlag = true;
            }
        }*/
    }
    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    /*绑定标志，绑定中标志；新绑定，换绑定，绑定中，各有不同文案提示*/
   // boolean bindFlag,bingingFlag;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_center, null, false);
            vh.tv_market_title = (TextView) convertView.findViewById(R.id.tv_market_title);
            vh.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            vh.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            vh.tv_bind = (TextView) convertView.findViewById(R.id.tv_bind);
            vh.iv_phone = convertView.findViewById(R.id.iv_phone);
            vh.tv_state = (TextView) convertView.findViewById(R.id.tv_state);
            vh.iv_logo = (ImageView) convertView.findViewById(R.id.iv_logo);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        CenterListEntity bean = list.get(position);
        if(list.indexOf(bean)==0){
            vh.tv_market_title.setVisibility(View.VISIBLE);
        }else {
            if(list.get(list.indexOf(bean)-1).getId().equals(bean.getId())){
                vh.tv_market_title.setVisibility(View.GONE);
            }else {
                vh.tv_market_title.setVisibility(View.VISIBLE);
            }
        }
        vh.tv_market_title.setText(bean.getName());
        vh.tv_title.setText(bean.getStore().getName());

        vh.tv_content.setText(bean.getStore().getDescription());
        String url = bean.getStore().getLogoUrl();
        ImageLoader.getInstance().displayImage(url, new ImageViewAware(vh.iv_logo), BaseApplication.getCenterOptions(url) , null, null, null);

        if(bean.getStore()==null||bean.getStore().getBindingStatus()==null||bean.getStore().getBindingStatus().equals("1")){
            vh.tv_state.setVisibility(View.INVISIBLE);
            vh.tv_bind.setVisibility(View.VISIBLE);
        }else if(bean.getStore().getBindingStatus().equals("2")){
            vh.tv_state.setText("绑定中");
            vh.tv_state.setVisibility(View.VISIBLE);
            vh.tv_bind.setVisibility(View.INVISIBLE);
        }else {
            vh.tv_state.setText("已绑定");
            vh.tv_state.setVisibility(View.VISIBLE);
            vh.tv_bind.setVisibility(View.INVISIBLE);
        }
        vh.tv_bind.setTag(R.id.item_key_position,position);
        vh.iv_phone.setTag(R.id.item_key_position, position);

        vh.iv_phone.setOnClickListener(this);
        vh.tv_bind.setOnClickListener(this);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_position);
        if (o != null && o instanceof Integer) {
                final int position = (int) o;
                if (v.getId() == R.id.iv_phone) {
                    Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + list.get(position).getStore().getManagerPhone()));//调出来拨打页面；
                    context.startActivity(call);
                } else if (v.getId() == R.id.tv_bind) {/*您正在申请绑定“%@”\n 申请后需配送中心通过 \n\n如有疑问，可联系配送中心*/
                    CustomDialog.Builder builder = new CustomDialog.Builder(context);

/*                    builder.setMessage("您正在申请绑定\"" + list.get(position).getStore().getName() + "\",\n申请后需配送中心通过\n\n如有疑问，可联系配送中心");//设置显示的内容
                    builder.setPositiveButton("确认绑定", new DialogInterface.OnClickListener() {//添加确定按钮
                        @Override
                        public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                            ((CenterListActivity)context).mHttpSure(list.get(position).getStore().getId());
                            dialog.dismiss();
                        }
                    });
                    builder.setNegativeButton("我再想想", new DialogInterface.OnClickListener() {//添加返回按钮
                        @Override
                        public void onClick(DialogInterface dialog, int which) {//响应事件
                            dialog.dismiss();
                        }

                    });*/
                    builder.setMessage("您正在申请绑定\"" + list.get(position).getStore().getName() + "\"，\n是否继续？");//设置显示的内容

                    builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {//添加确定按钮
                        @Override
                        public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                            ((CenterListActivity)context).mHttpSure(list.get(position).getStore().getId());
                            dialog.dismiss();
                        }
                    });
                    builder.setNegativeButton("联系配送中心", new DialogInterface.OnClickListener() {//添加返回按钮
                        @Override
                        public void onClick(DialogInterface dialog, int which) {//响应事件
                            Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + list.get(position).getStore().getManagerPhone()));//调出来拨打页面；
                            context.startActivity(call);
                            //dialog.dismiss();
                        }

                    });


                    builder.createCenterBind(R.layout.dialog_round_layout).show();
                }
        }
    }

    public class ViewHolder {
        TextView tv_market_title;
        TextView tv_title;
        TextView tv_content;
        TextView tv_bind;
        View iv_phone;
        TextView tv_state;
        ImageView iv_logo;
    }
}
